<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>规则列表</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="autumn-center">
      <!-- 搜索栏 -->
      <div class="query-form">
        <autumn-search :modelValue="queryInfo" @search="search">
          <template #default>
            <el-form-item prop="code" label="标识">
              <el-input v-model="queryInfo.code" />
            </el-form-item>
            <el-form-item prop="name" label="名称">
              <el-input v-model="queryInfo.name" />
            </el-form-item>
            <el-form-item prop="isEnable" label="启用">
              <el-select v-model="queryInfo.isEnable" placeholder="请选择">
                <el-option label="是" value="1" />
                <el-option label="否" value="0" />
              </el-select>
            </el-form-item>
          </template>
        </autumn-search>
      </div>

      <!-- 数据表格 -->
      <div class="main-body top">
        <autumn-table :page="page" @changePage="changePage">
          <template #button>
            <el-button type="primary" icon="el-icon-plus" @click="toView" v-if="$hasPermission('system:barcode:add')">新增
            </el-button>
          </template>
          <template #column>
            <el-table-column type="index" label="#" fixed="left" align="center">
              <template #default="scope">
                {{ initIndex(scope.$index) }}
              </template>
            </el-table-column>
            <el-table-column prop="code" label="标识" />
            <el-table-column prop="name" label="名称" />
            <el-table-column label="启用" align="center" width="100">
              <template #default="scope">
                <el-switch v-model="scope.row.isEnable" :active-value="1" :inactive-value="0"
                  :disabled="!$hasPermission('system:barcode:enable&system:barcode:disable')"
                  @change="changeEnabled(scope.row)" />
              </template>
            </el-table-column>
            <el-table-column prop="createName" label="创建人" width="100" />
            <el-table-column prop="createTime" label="创建时间" align="center" width="150"
              :formatter="(row: any) => $moment(row.createTime, 'YYYY-MM-DD HH:mm')" />
            <el-table-column label="操作" fixed="right" align="center" width="160">
              <template #default="scope">
                <el-button-group>
                  <el-button type="info" icon="el-icon-view" size="small" title="查看" @click="toView(scope.row, '1')" />
                  <el-button type="primary" icon="el-icon-edit" size="small" title="编辑" @click="toView(scope.row)"
                    v-if="$hasPermission('system:barcode:update')" />
                  <el-button type="warning" icon="el-icon-tickets" size="small" title="管理子项" @click="toRule(scope.row)"
                    v-if="$hasPermission('system:barcode-rule:list')" />
                </el-button-group>
              </template>
            </el-table-column>
          </template>
        </autumn-table>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { pageBarcode, enableBarcode, disableBarcode } from 'src/api/system/barcode'
import useTable from 'src/hooks/useTable'
import { $hasPermission, $moment } from 'src/hooks/useCommon'

const { page, initIndex } = useTable()

const router = useRouter()

// 分页查询
const queryInfo = reactive({
  code: '',
  name: '',
  isEnable: ''
})
const changePage = (pageNumber: number, pageSize: number) => {
  page.pageNumber = pageNumber
  page.pageSize = pageSize
  pageBarcode({ ...queryInfo, pageNumber, pageSize }).then(({ data }) => {
    page.data = data.list
    page.total = data.total
  })
}
const search = () => {
  changePage(1, 10)
}
search()

// 启/禁用
const changeEnabled = (row: any) => {
  const id = row.id
  const isEnable = row.isEnable
  if (isEnable) {
    enableBarcode(id).then(() => {
      search()
    })
  } else {
    disableBarcode(id).then(() => {
      search()
    })
  }
}

// 查看
const toView = async (row: any, type?: string) => {
  const id = row.id
  router.push({
    path: type ? '/system/barcode/barcodeView' : '/system/barcode/barcodeForm',
    query: { id }
  })
}

// 角色授权
const toRule = (row: any) => {
  const id = row.id
  router.push({
    path: '/system/barcodeRule/barcodeRuleList',
    query: { barcodeId: id }
  })
}
</script>

<style lang="scss" scoped>
</style>
